package com.app;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.app.producer.vo.Demo;
import com.app.rabbit.DirectProducer;
import com.app.rabbit.FanoutProducer;
import com.app.rabbit.JsonProducer;
import com.app.rabbit.TTLProducer;
import com.app.rabbit.Test1Producer;
import com.app.rabbit.TopicProducer;

//2020-10-11 15:53:45
@RequestMapping(value = IndexController.PATH, name = "直播API")
@RestController("LiveApiController")
public class IndexController {
	private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(IndexController.class);
	public static final String PATH = "index";

	@Autowired
	Test1Producer test1Producer;
	@Autowired
	FanoutProducer fanoutProducer;
	@Autowired
	DirectProducer directProducer;
	@Autowired
	TTLProducer ttlProducer;
	@Autowired
	TopicProducer topicProducer;
	
	@Autowired
	JsonProducer jsonProducer;
	

	@PostMapping(value = "/sendTest1")
	public Map<String, Object> sendMessage(@RequestBody Map<String, Object> ps) {
		String msg = String.format("%s @%s", (String) ps.get("msg"), LocalDateTime.now());
		test1Producer.sendTest1(msg);
		return Map.of("success", true, "message", "成功");
	}

	@PostMapping(value = "/sendToFanout")
	public Map<String, Object> sendToFanout(@RequestBody Map<String, Object> ps) {
		String msg = String.format("%s @%s", (String) ps.get("msg"), LocalDateTime.now());
		fanoutProducer.sendToFanout(msg);
		return Map.of("success", true, "message", "成功");
	}

	@PostMapping(value = "/sendToDirect")
	public Map<String, Object> sendToDirect(@RequestBody Map<String, Object> ps) {
		String msg = String.format("%s @%s", (String) ps.get("msg"), LocalDateTime.now());
		directProducer.sendToDirect((String) ps.get("key"), msg);
		return Map.of("success", true, "message", "成功");
	}

	@PostMapping(value = "/sendToDelay10Queue")
	public Map<String, Object> sendToDelay10Queue(@RequestBody Map<String, Object> ps) {
		String msg = String.format("%s @%s", (String) ps.get("msg"), LocalDateTime.now());
		ttlProducer.sendToDelay10Queue(msg);
		return Map.of("success", true, "message", "成功");
	}

	@PostMapping(value = "/sendToTopic")
	public Map<String, Object> sendToTopic(@RequestBody Map<String, Object> ps) {
		String msg = String.format("%s @%s", (String) ps.get("msg"), LocalDateTime.now());
		topicProducer.sendToTopic((String) ps.get("key"), msg);
		return Map.of("success", true, "message", "成功");
	}
	
	
	//发送json JacksonConfig.jackson2JsonMessageConverter 配置
	@PostMapping(value = "/sendTestJson")
	public Map<String, Object> sendTestJson(@RequestBody Map<String, Object> ps) {
		String msg = String.format("%s @%s", (String) ps.get("msg"), LocalDateTime.now());
		Demo d=new Demo();
		d.setId(UUID.randomUUID().toString());
		d.setName(msg);
		d.setAge(System.currentTimeMillis());
		jsonProducer.sendTestJson(d);
		return Map.of("success", true, "message", "成功","data",d);
	}

}